package com.tianbo.elfii.client.common;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;

import com.tianbo.elfii.client.common.ElfiiCommonUtil.FeedBackType;
import com.tianbo.elfii.client.common.LanStaticIpPane.ConfirmBtnListener;
import com.tianbo.elfii.client.common.wlanmanagement.BaseDialogPane;

public class WlanStaticIpPane extends BaseDialogPane {

	public ElfiiCommonKeyboard keyboard = null;
	public ElfiiCommonConfigNetWifi wifiConfig = null;
	public IPSetDialog dialog = null; 

	public WlanStaticIpPane(Object obj) {
		super();
		// TODO Auto-generated constructor stub
		
		wifiConfig = (ElfiiCommonConfigNetWifi)obj;
	
		init();
		
		dialog.setOpaque(false);
	}
	
	public void init(){
		initStaticIpDialog();
		initKeyboardPane();
		
	}
	
	public void initStaticIpDialog(){
		dialog = new IPSetDialog(new WlanAP());
		dialog.setBounds(287, 30, 450, 350);
		dialog.init();
		dialog.confirmBtn.addActionListener(new ConfirmBtnListener());
		dialog.cancelBtn.addActionListener(new CancelBtnListener());
		this.add(dialog);
	}

	public void initKeyboardPane() {
		keyboard = new ElfiiCommonLoginKeyboard();
		

		JPanel keyPanel = keyboard.createKeyPanel();
		keyPanel.setOpaque(false);

		this.add(keyPanel);
		
		keyPanel.setBounds(150, 420, 735, 300);

		keyboard.createInfoPanel();

		dialog.keyboard = keyboard;
		
		keyboard.isDigiOnly = true;
	}
	
	public void showDialog(){
		super.showDialog();
		dialog.staticIpFields[0][0].requestFocus();
	}
	
	public class ConfirmBtnListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stu
			if(dialog.isUseStaticIP){
				ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
				if(dialog.getInputValidity()){
					if(ElfiiCommonConfigUtil.isWlanAutoIP){
						ElfiiCommonConfigUtil.setWlanAutoIP(false);
						ElfiiCommonConfigUtil.wlanStaticIP = dialog.getInputIP();
						
						if(ElfiiCommonConfigUtil.curConnectedAp!=null){
							wifiConfig.selectedApPanel = wifiConfig.connectedApPanel;
							WlanConnectingThread thread = new WlanConnectingThread(wifiConfig);
							wifiConfig.selectedApPanel.ap.setCurStat(APSTAT.CONNECTING);
							thread.start();
						}
					}
					else{
						IPInfo tmpIPInfo = dialog.getInputIP();
						if(!ElfiiCommonConfigUtil.wlanStaticIP.equals(tmpIPInfo)){
							ElfiiCommonConfigUtil.wlanStaticIP = dialog.getInputIP();
							if(ElfiiCommonConfigUtil.curConnectedAp!=null){
								wifiConfig.selectedApPanel = wifiConfig.connectedApPanel;
								WlanConnectingThread thread = new WlanConnectingThread(wifiConfig);
								wifiConfig.selectedApPanel.ap.setCurStat(APSTAT.CONNECTING);
								thread.start();
							}
						}
					}
					
					saveWlanStaticIP();
					
					mainFrame.setContentPane(backPanel);
				}
			}
			else{
				

				ElfiiCommonConfigUtil.wlanStaticIP = dialog.getInputIP();
				
				if(!ElfiiCommonConfigUtil.isWlanAutoIP){
					if(ElfiiCommonConfigUtil.curConnectedAp!=null){
						wifiConfig.selectedApPanel = wifiConfig.connectedApPanel;
						WlanConnectingThread thread = new WlanConnectingThread(wifiConfig);
						wifiConfig.selectedApPanel.ap.setCurStat(APSTAT.CONNECTING);
						thread.start();
					}
				}
				
				saveWlanStaticIP();
				mainFrame.setContentPane(backPanel);
			}
			
		}
	}
	
	
	public class CancelBtnListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stu
			ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
			mainFrame.setContentPane(backPanel);
		}
	}
	
	public void saveWlanStaticIP(){

		ElfiiCommonConfigUtil.setWlanAutoIP(!dialog.isUseStaticIP);
		ElfiiCommonConfigUtil.saveWlanStaticIP();
	}

}
